//
//  BBYRetailViewController.m
//  BearSiX
//
//  Created by Tom on 16/3/25.
//  Copyright © 2016年 Tom. All rights reserved.
//

#import "BBYRetailViewController.h"
#import "BBXFTypeViewController.h"
#import "BBYHandAreaViewController.h"
@interface BBYRetailViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UIView *dataChooseView;//时间控制器
    UITextField *_nameField;
    UITextField *_phoneField;
    UIButton *boyBtn;
    UIButton *weddBtn;
    NSMutableArray *_allImages;
    NSMutableArray *_allTexts;
    NSInteger _row;
}
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIDatePicker *dataPicker;
@end

@implementation BBYRetailViewController
-(UITableView *)tableView{

    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, WINSIZEWIDTH/7.8+64, WINSIZEWIDTH, WINSIZEHEIGHT-WINSIZEWIDTH/7.8-64)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WINSIZEWIDTH, WINSIZEWIDTH/2)];
        _tableView.tableFooterView = footerView;
        BButton *saveBtn = [[BButton alloc]initWithFrame:CGRectMake(WINSIZEWIDTH/3.4, WINSIZEWIDTH/12, WINSIZEWIDTH-WINSIZEWIDTH/1.7, WINSIZEWIDTH/9) color:[UIColor colorWithHexString:@"f05709"] style:(BButtonStyleBootstrapV2)];
        [saveBtn addTarget:self action:@selector(saveDataClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [saveBtn setTitle:@"保存" forState:(UIControlStateNormal)];
        [saveBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        saveBtn.titleLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/20];
        [footerView addSubview:saveBtn];
    }
    return _tableView;
}
-(UIDatePicker *)dataPicker{

    if (!_dataPicker) {
        //_dataPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, WINSIZEWIDTH/2, WINSIZEWIDTH, WINSIZEWIDTH/1.5)];
        _dataPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, WINSIZEWIDTH/10, WINSIZEWIDTH, WINSIZEWIDTH/1.5)];
       // _dataPicker.center = CGPointMake(WINSIZEWIDTH/2, WINSIZEWIDTH/1.2);
        NSLocale *cale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
        _dataPicker.backgroundColor = [UIColor whiteColor];
        _dataPicker.locale = cale;
               _dataPicker.datePickerMode = UIDatePickerModeDateAndTime;
        [_dataPicker addTarget:self action:@selector(chooseData:) forControlEvents:(UIControlEventValueChanged)];
    }
    return _dataPicker;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"门市开单";
    self.view.backgroundColor = [UIColor colorWithHexString:@"e7e6f1"];
    [self initDataSource];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"saoyisao_y"] style:(UIBarButtonItemStyleDone) target:self action:@selector(saoYiSao:)];
    _row = 100;
    [self createUI];

}
-(void)saoYiSao:(UIBarButtonItem *)sender{

}
-(void)createUI{

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(WINSIZEWIDTH/20, WINSIZEWIDTH/50+64, WINSIZEWIDTH-WINSIZEWIDTH/10, WINSIZEWIDTH/7.8-WINSIZEWIDTH/25)];
    view.layer.cornerRadius = WINSIZEWIDTH/60;
    view.layer.borderColor = [UIColor colorWithHexString:@"e22849"].CGColor;
    view.layer.masksToBounds = YES;
    view.layer.borderWidth = 1.5f;
    view.backgroundColor = [UIColor colorWithHexString:@"e22849"];
   weddBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, view.width/2, view.height)];
    [weddBtn setTitle:@"婚纱客户" forState:(UIControlStateNormal)];
    [weddBtn setTitleColor:[UIColor colorWithHexString:@"e22849"] forState:(UIControlStateNormal)];
    [weddBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
    weddBtn.titleLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/22];
    weddBtn.selected = YES;
    [weddBtn addTarget:self action:@selector(scaleTableView:) forControlEvents:(UIControlEventTouchUpInside)];
    weddBtn.backgroundColor = [UIColor clearColor];
    boyBtn = [[UIButton alloc]initWithFrame:CGRectMake(view.width/2, 0, weddBtn.width, weddBtn.height)];
    [boyBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
    [boyBtn setTitleColor:[UIColor colorWithHexString:@"e22849"] forState:(UIControlStateNormal)];
    [boyBtn setTitle:@"儿童客户" forState:(UIControlStateNormal)];
    boyBtn.titleLabel.font = weddBtn.titleLabel.font;
    [boyBtn addTarget:self action:@selector(scaleTableView:) forControlEvents:(UIControlEventTouchUpInside)];
    boyBtn.backgroundColor = [UIColor colorWithHexString:@"f0effa"];

    
    [view addSubview:weddBtn];
    [view addSubview:boyBtn];
    
    UIView *horView = [[UIView alloc]initWithFrame:CGRectMake(0, self.tableView.y, WINSIZEWIDTH, 1.5)];
    horView.backgroundColor = [UIColor colorWithHexString:@"dcdcdf"];
    
    
    //时间控制器界面
    dataChooseView = [[UIView alloc]initWithFrame:CGRectMake(0, WINSIZEHEIGHT, WINSIZEWIDTH, self.dataPicker.height+WINSIZEWIDTH/10)];
    BButton *cancleBtn = [[BButton alloc]initWithFrame:CGRectMake(WINSIZEWIDTH/8, WINSIZEWIDTH/16-WINSIZEWIDTH/20, WINSIZEWIDTH/4, WINSIZEWIDTH/10) color:[UIColor colorWithHexString:@"6e6f71"] style:(BButtonStyleBootstrapV3)];
    [cancleBtn setTitle:@"取消"forState:(UIControlStateNormal)];
    [cancleBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/22];
    [cancleBtn addTarget:self action:@selector(dataPickerHide:) forControlEvents:(UIControlEventTouchUpInside)];
    
    BButton *okBtn = [[BButton alloc]initWithFrame:CGRectMake(cancleBtn.x+WINSIZEWIDTH/2, cancleBtn.y, cancleBtn.width, cancleBtn.height) color:[UIColor colorWithHexString:@"f05709"] style:(BButtonStyleBootstrapV3)];
    [okBtn setTitle:@"确认" forState:(UIControlStateNormal)];
    [okBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    okBtn.titleLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/22];
    [okBtn addTarget:self action:@selector(dataPickerChoose:) forControlEvents:(UIControlEventTouchUpInside)];
    
    dataChooseView.backgroundColor = [UIColor whiteColor];
    [dataChooseView addSubview:self.dataPicker];
    [dataChooseView addSubview:cancleBtn];
    [dataChooseView addSubview:okBtn];
    [self.view addSubview:view];
    [self.view addSubview:self.tableView];
    [self.view addSubview:horView];
    [self.view addSubview:dataChooseView];
    NSLog(@"+++%@",dataChooseView);
    //[self.view addSubview:self.dataPicker];
}
#pragma mark -- 时间控制器的操作
//取消
-(void)dataPickerHide:(BButton *)sender{

    [UIView animateKeyframesWithDuration:0.3 delay:0.1 options:(UIViewKeyframeAnimationOptionOverrideInheritedDuration) animations:^{
        dataChooseView.y = WINSIZEHEIGHT;
    } completion:nil];
}
//确定
-(void)dataPickerChoose:(BButton *)sender{

    [UIView animateKeyframesWithDuration:0.3 delay:0.1 options:(UIViewKeyframeAnimationOptionOverrideInheritedDuration) animations:^{
        dataChooseView.y = WINSIZEHEIGHT;
    } completion:^(BOOL finished) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSDate *date = [NSDate date];
        NSDate *str = [date laterDate:self.dataPicker.date];
        NSString *dataStr = [formatter stringFromDate:self.dataPicker.date];
        NSLog(@"+++datapicker:%@",self.dataPicker.date);
            _allTexts[1] = [NSString stringWithFormat:@"%@",dataStr];
        
                [self.tableView reloadData];
    }];
}
-(void)scaleTableView:(UIButton *)sender{

    boyBtn.selected = NO;
    boyBtn.backgroundColor = [UIColor colorWithHexString:@"f0effa"];
    weddBtn.backgroundColor = [UIColor colorWithHexString:@"f0effa"];
    weddBtn.selected = NO;
    sender.selected = YES;
    sender.backgroundColor = [UIColor clearColor];
}
#pragma mark -- tableviewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _allTexts.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return WINSIZEWIDTH/6.7;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:nil];
    cell.imageView.image = [UIImage imageNamed:_allImages[indexPath.row]];
    cell.textLabel.text = _allTexts[indexPath.row];
    cell.textLabel.font =[UIFont systemFontOfSize:WINSIZEWIDTH/20];
    if (indexPath.row>=5) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if(indexPath.row==3){
    
        cell.textLabel.text = @"";
        _nameField = [[UITextField alloc]initWithFrame:CGRectMake(WINSIZEWIDTH/5.9, 0, WINSIZEWIDTH/2, WINSIZEWIDTH/6.7)];
        NSString *nameStr = [NSString stringWithFormat:@"%@",_nameField.text];
        _nameField.placeholder = nameStr.length<1? @"请输入姓名":_nameField.text;
        _nameField.font = [UIFont systemFontOfSize:WINSIZEWIDTH/20];
        _nameField.delegate = self;
        _nameField.returnKeyType = UIReturnKeyDone;
        [cell addSubview:_nameField];
        cell.selected = NO;
    }else if (indexPath.row==4){
    
        cell.textLabel.text = @"";
        _phoneField = [[UITextField alloc]initWithFrame:CGRectMake(_nameField.x, 0, WINSIZEWIDTH/2, WINSIZEWIDTH/6.7)];
        NSString *phoneStr = [NSString stringWithFormat:@"%@",_phoneField.text];
        _phoneField.placeholder = phoneStr.length<1?@"请输入手机号":_phoneField.text;
        _phoneField.font = _nameField.font;
        _phoneField.delegate = self;
        _phoneField.returnKeyType = UIReturnKeyDone;
        [cell addSubview:_phoneField];
        cell.selected = NO;
    }
    
    return cell;


}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==1) {
        [UIView animateKeyframesWithDuration:0.3 delay:0.1 options:(UIViewKeyframeAnimationOptionOverrideInheritedDuration) animations:^{
            NSLog(@"+++%@",dataChooseView);
           dataChooseView.y = WINSIZEHEIGHT-self.dataPicker.height-WINSIZEWIDTH/8;
        } completion:nil];
    }else if (indexPath.row==4){
        _row = 4;
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"男" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            _allTexts[_row] = @"男";
            _row = 100;
            [self.tableView reloadData];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"女" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            _allTexts[_row] = @"女";
            _row = 100;
            [self.tableView reloadData];
        }];
        UIAlertAction *actionCancle = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        [alertVC addAction:action1];
        [alertVC addAction:action2];
        [alertVC addAction:actionCancle];
        [self presentViewController:alertVC animated:YES completion:nil];
    }else if (indexPath.row==5){
    
        BBXFTypeViewController *xftypeVC = [[BBXFTypeViewController alloc]init];
        [self.navigationController pushViewController:xftypeVC animated:YES];
    }else if(indexPath.row == 6){
        BBYHandAreaViewController *hadleVC = [[BBYHandAreaViewController alloc]init];
        [self.navigationController pushViewController:hadleVC animated:YES];
    }
}
-(void)chooseData:(id)sender{

    UIDatePicker *dataPicker = (UIDatePicker *)sender;
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *date = [NSDate date];
    NSDate *str = [date laterDate:dataPicker.date];
    NSLog(@"+++datapicker:%@",dataPicker.date);
}
-(void)saveDataClick:(BButton *)sender{

    
}
-(void)initDataSource{

    _allTexts = [@[@"北京施华洛婚纱摄影广渠门店",@"2015年11月26日 下午6:00",@"请输入姓名",@"请输入手机号",@"男",@"请选择消费类型",@"请选择接单点"]mutableCopy];
    _allImages = [@[@"m_gongsi_y",@"m_riqi_y",@"m_xingming_y",@"m_dianhua_y",@"m_xingbie_y",@"m_type_y",@"m_dizhi_y"]mutableCopy];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- textField delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionTop) animated:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{

   // _nameField.text = @"heool";
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES];
    [self.view endEditing:YES];
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
